﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditViand : AdminPageBase
    {
        private ViandInfo _viandInfo = new ViandInfo();
        private readonly ViandController _viandController = new ViandController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["ViandId"]))
                _viandInfo = _viandController.GetViand(int.Parse(Request.QueryString["ViandId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadAdminUser();

            if (_viandInfo.ViandId != -1)
                RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListViand.aspx?xml=Viand");
        }

        private void SaveData()
        {
            _viandInfo.ViandName = txtViandName.Text;
            _viandInfo.Status = int.Parse(ddlStatus.SelectedValue);
            if (_viandInfo.ViandId == -1)
            {
                _viandInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _viandInfo.CreatedDate = DateTime.Now;
                _viandController.InsertViand(_viandInfo);
            }
            else
            {
                _viandInfo.UpdatedBy = UserId;
                _viandInfo.UpdatedDate = DateTime.Now;
                _viandController.UpdateViand(_viandInfo);
            }
            Response.Redirect("/Pages/ListViand.aspx?xml=Viand");
        }

        private void RenderForm()
        {
            txtViandName.Text = _viandInfo.ViandName;
            ddlStatus.SelectedValue = _viandInfo.Status.ToString();
            ddlAdminUser.SelectedValue = _viandInfo.CreatedBy.ToString();
        }

        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }

        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}